package com.sgb.kjwl.view.ui.fragment.self;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentCommonListBinding;
import com.sgb.kjwl.model.entity.BidEntity;
import com.sgb.kjwl.view.ui.activity.pick.QuoteImmediatelyActivity;
import com.sgb.kjwl.view.ui.activity.self.bid.BidQuoteActivity;
import com.sgb.kjwl.view.ui.adapter.self.bid.BidGcAdapter;
import com.sgb.kjwl.view.ui.adapter.self.bid.BidLaborAdapter;
import com.sgb.kjwl.view.ui.adapter.self.bid.BidMachineAdapter;
import com.sgb.kjwl.view.ui.adapter.self.bid.BidMaterialAdapter;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * author：zhao
 * 个人中心-投标报价-报价查询中的四个列表（0 材料分类，1 机械设备，2 工程分包，3 劳务分包）
 */
public class BidQuoteListFragment extends AppFragment {

    private FragmentCommonListBinding binding;
    private BidQuoteActivity activity;
    private BidMaterialAdapter materialAdapter;
    private BidMachineAdapter machineAdapter;
    private BidGcAdapter gcAdapter;
    private BidLaborAdapter laborAdapter;
    private boolean isLoadMore;
    private int type;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_common_list, container, false);
        if (getArguments() != null) {
            type = getArguments().getInt("type");
        }
        EventBus.getDefault().register(this);
        initView();
        return binding.getRoot();
    }

    public static BidQuoteListFragment newInstance(int type) {
        BidQuoteListFragment fragment = new BidQuoteListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    private void initView() {
        if (type == 0) {
            materialAdapter = new BidMaterialAdapter(activity, null);
            binding.list.setLayoutManager(new LinearLayoutManager(activity));
            binding.list.setAdapter(materialAdapter);
            materialAdapter.IOffer((entity, id) -> {
                /*SharedPreferenceUtil.init(getActivity());
                String token = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, "");
                if (!"".equals(token)) {*/
//                    if ("1".equals(entity.getStatusCode())) {
//                        AuthActivity.start(getActivity());
//                    } else if ("3".equals(entity.getStatusCode())) {
                    QuoteImmediatelyActivity.startAgain(getActivity(), entity, id);
//                    }
                /*} else {
                    activity.jumpLogin();
                }*/

            });

            binding.list.setLoadingListener(new XRecyclerView.LoadingListener() {
                @Override
                public void onRefresh() {
                    binding.list.setLoadingMoreEnabled(true);
                    isLoadMore = false;
                    activity.getMaterial(false);
                }

                @Override
                public void onLoadMore() {
                    isLoadMore = true;
                    activity.getMaterial(true);

                }
            });
        } else if (type == 1) {
            machineAdapter = new BidMachineAdapter(activity, null);
            binding.list.setLayoutManager(new LinearLayoutManager(activity));
            binding.list.setAdapter(machineAdapter);

            binding.list.setLoadingListener(new XRecyclerView.LoadingListener() {
                @Override
                public void onRefresh() {
                    binding.list.setLoadingMoreEnabled(true);
                    isLoadMore = false;
                    activity.getMachine(false);
                }

                @Override
                public void onLoadMore() {
                    isLoadMore = true;
                    activity.getMachine(true);
                }
            });
        } else if (type == 2) {
            gcAdapter = new BidGcAdapter(activity, null);
            binding.list.setLayoutManager(new LinearLayoutManager(activity));
            binding.list.setAdapter(gcAdapter);

            binding.list.setLoadingListener(new XRecyclerView.LoadingListener() {
                @Override
                public void onRefresh() {
                    binding.list.setLoadingMoreEnabled(true);
                    isLoadMore = false;
                    activity.getGc(false);
                }

                @Override
                public void onLoadMore() {
                    isLoadMore = true;
                    activity.getGc(true);
                }
            });
        } else if (type == 3) {
            laborAdapter = new BidLaborAdapter(activity, null);
            binding.list.setLayoutManager(new LinearLayoutManager(activity));
            binding.list.setAdapter(laborAdapter);

            binding.list.setLoadingListener(new XRecyclerView.LoadingListener() {
                @Override
                public void onRefresh() {
                    binding.list.setLoadingMoreEnabled(true);
                    isLoadMore = false;
                    activity.getLabor(false);
                }

                @Override
                public void onLoadMore() {
                    isLoadMore = true;
                    activity.getLabor(true);
                }
            });
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof BidQuoteActivity) {
            activity = (BidQuoteActivity) context;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEvent(List<BidEntity> data) {
        if (type == 0 && isVisible()) {
            showMaterialList(data);
        } else if (type == 1 && isVisible()) {
            showMachineList(data);
        } else if (type == 2 && isVisible()) {
            showGcList(data);
        } else if (type == 3 && isVisible()) {
            showLaborList(data);
        }
    }

    private void showLaborList(List<BidEntity> data) {
        if (isLoadMore) {
            laborAdapter.addDatas(data);
            binding.list.loadMoreComplete();
        } else {
            laborAdapter.setDatas(data);
            binding.list.refreshComplete();
            binding.list.setLoadingMoreEnabled(true);
        }
    }

    private void showGcList(List<BidEntity> data) {
        if (isLoadMore) {
            gcAdapter.addDatas(data);
            binding.list.loadMoreComplete();
        } else {
            gcAdapter.setDatas(data);
            binding.list.refreshComplete();
            binding.list.setLoadingMoreEnabled(true);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(String s) {
        if (s.equals("EMPTY") && isVisible()) {
            if (isLoadMore) {
                binding.list.loadMoreComplete();
            } else {
                binding.list.refreshComplete();
                binding.empty.setVisibility(View.VISIBLE);
                binding.list.setLoadingMoreEnabled(true);
            }

        }
    }

    private void showMaterialList(List<BidEntity> data) {
        if (isLoadMore) {
            materialAdapter.addDatas(data);
            binding.list.loadMoreComplete();
        } else {
            materialAdapter.setDatas(data);
            binding.list.refreshComplete();
            binding.list.setLoadingMoreEnabled(true);
        }
    }

    private void showMachineList(List<BidEntity> data) {
        if (isLoadMore) {
            machineAdapter.addDatas(data);
            binding.list.loadMoreComplete();
        } else {
            machineAdapter.setDatas(data);
            binding.list.refreshComplete();
            binding.list.setLoadingMoreEnabled(true);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
